<template>
  <div>
    <c-table
      v-if="height"
      :loading="loading"
      :height="height"
      :table-column="systemCoulmn"
      :table-data="list"
      is-selection
      :is-delete="false"
      :operation="!disabled"
      @on-save="onSave"
      @modify="modifyHandle"
      @focus="focusHandle"
    />
    <c-table
      v-else
      :loading="loading"
      :table-column="systemCoulmn"
      :table-data="list"
      is-selection
      :is-delete="false"
      :operation="!disabled"
      @on-save="onSave"
      @modify="modifyHandle"
      @focus="focusHandle"
    />
    <issue-tracking-dialog
      v-if="issueTrackingData.show"
      :params="issueTrackingData.params"
      @close="issueTrackingCloseHandle"
    />
  </div>
</template>

<script>
import IssueTrackingDialog from "@/components/IssueTrackingDialog.vue";
import { systemCoulmn } from "../data";
import { overviewApis } from "@/apis";

export default {
  components: {IssueTrackingDialog},
  props: {
    loading: {
      type: Boolean,
      default: false
    },
    list: {
      type: Array,
      default() {
        return []
      }
    },
    height: {
      type: String,
      default: null
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      systemCoulmn,
      issueTrackingData: {
        show: false,
        params: {
          billId: 0,
          type: "",
        },
      },
      type: {
        complete: "READY_WCXTZHSY",
        inspect: "JJJYS",
        review: "CCPS",
        sysReady: "XTJF",
      },
    }
  }, 
  methods: {
    async onSave(row) {
      await overviewApis.progressReadySave(row);
      this.refresh()
    },
    modifyHandle(key, row, isComplete) {
      if (this.disabled) {
        return
      }
      this.cacheModifyData = {
        key,
        row
      }
      /**
       * 
       * EXP_WCXTZHSY("EXP_WCXTZHSY", "综合实验进度-完成系统综合试验"),
       * JJYS("JJJYS", "军检及验收"),
       * CCPS("CCPS", "出厂评审"),
      *XTJF("XTJF", "系统交付");

      */ 
      if (isComplete) {
        overviewApis.saveProgressRecord({
          billId: row.id,
          type: this.type[key],
          complete: 1
        }).then(() => {
          // this.refresh()
          this.issueTrackingRefreshRow()
        })
      } else {
        this.issueTrackingData.show = true;
        this.issueTrackingData.params.billId = row.id;
        this.issueTrackingData.params.type = this.type[key];
      }
    },
    issueTrackingCloseHandle(refresh) {
      if (refresh) {
        // this.refresh()
        this.issueTrackingRefreshRow()
      }
      this.issueTrackingData.show = false;
    },
    async focusHandle(row) {
      if (this.disabled) {
        return
      }
      await overviewApis.progressReadyFocus({ id: row.id });
      this.refresh()
    },
    refresh() {
      this.$emit('refresh')
    },
    issueTrackingRefreshRow() {
      const {key, row} = this.cacheModifyData
      const valueMap = this.systemCoulmn[key].valueMap
      overviewApis.progressReadyFindPageList({
          id: row.id
        })
        .then(({ data }) => {
          const { list } = data
          const newData = list[0]
          Object.assign(row, {
            [valueMap.value]: newData[valueMap.value],
            [valueMap.progress]: newData[valueMap.progress],
            [valueMap.count]: newData[valueMap.count],
            readyColor: newData['readyColor'],
            readyProp: newData['readyProp'],
          })
        })
    }
  }
}
</script>

<style>

</style>